package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ddyx.common.util.BeanUtil;
import com.ddyx.common.util.DateUtil;
import com.ddyx.common.util.RequestForm;
import com.ddyx.common.util.ResponseForm;
import com.ddyx.common.util.ResponseJson;
import com.ddyx.entity.Task;
import com.ddyx.entity.TaskGroup;
import com.ddyx.mapper.TaskGroupMapper;
import com.ddyx.mapper.TaskMapper;
import com.ddyx.service.ITaskGroupService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("iTaskGroupService")
public class TaskGroupServiceImpl implements ITaskGroupService {

	@Resource
	TaskMapper taskMapper;
	
	@Resource
	TaskGroupMapper taskGroupMapper;

	@SuppressWarnings("unchecked")
	@Override
	public String getTaskGroupInfoByIdPage(RequestForm requestForm) {
		Map<String, Object> params = (Map<String, Object>) requestForm.getData();
		int size = params.get("size") == null ? 0 : Integer.valueOf(params.get("size") + "");
		int page = params.get("page") == null ? 0 : Integer.valueOf(params.get("page") + "");
		int start = 0;
		if (size < 1)
			size = 20;// 默认每页20条
		if (page < 1)
			page = 1;// 默认第1页
		start = (page - 1) * size;
		params.put("start", start);
		params.put("size", size);
		params.put("placeId", requestForm.getPlaceId());
		int total = taskGroupMapper.getTaskGroupInfoCount(params);
		List<Map<String,Object>> taskList = taskGroupMapper.getTaskGroupInfoById(params);
		return ResponseJson.getInstance().setPage(page).setSize(size).setTotal(total).setData(taskList).toString();
	}

	@Override
	public ResponseForm addTaskGroup(RequestForm requestForm) {
		String userId = requestForm.getUserId();
		String placeId = requestForm.getPlaceId();
		ResponseForm res = new ResponseForm();
		try {
			TaskGroup taskGroup = BeanUtil.parseBean(requestForm, new TaskGroup());
			// 设置默认属性
			taskGroup.setCreateUser(userId);
			taskGroup.setCreateTime(DateUtil.format_ymd_hms.format(new Date()));
			taskGroup.setTaskGroup(BeanUtil.createId());
			taskGroup.setPlaceId(placeId);
			int result = taskGroupMapper.addTaskGroup(taskGroup);
			if (result < 0) {
				res.setStatus(false);
				res.setMessage("添加任务组失败");
			}
		} catch (Exception e) {
			res.setStatus(false);
			res.setMessage("参数解析错误");
			log.error("添加任务组失败 " + e);
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm deleteTaskGroup(RequestForm requestForm) {
		ResponseForm res = new ResponseForm();
		try {
			Map<String, Object> params = (Map<String, Object>) requestForm.getData();
			int result = taskGroupMapper.deleteTaskGroup(params);
			if (result < 0) {
				res.setStatus(false);
				res.setMessage("删除任务组失败");
			}
		} catch (Exception e) {
			res.setStatus(false);
			res.setMessage("删除任务组失败");
			log.error("删除任务组失败" + e);
		}
		return res;
	}

	@Override
	public ResponseForm modifyTaskGroup(RequestForm requestForm) {
		ResponseForm res = new ResponseForm();
		try {
			TaskGroup taskGroup = BeanUtil.parseBean(requestForm, new TaskGroup());
			int result = taskGroupMapper.modifyTaskGroup(taskGroup);
			if (result < 0) {
				res.setStatus(false);
				res.setMessage("修改任务组失败");
			}
		} catch (Exception e) {
			res.setStatus(false);
			res.setMessage("参数解析错误");
			log.error("修改任务组失败" + e);
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getTaskByGroupId(RequestForm requestForm) {
		Map<String, Object> params = (Map<String, Object>) requestForm.getData();
		int size = params.get("size") == null ? 0 : Integer.valueOf(params.get("size") + "");
		int page = params.get("page") == null ? 0 : Integer.valueOf(params.get("page") + "");
		int start = 0;
		if (size < 1)
			size = 20;// 默认每页20条
		if (page < 1)
			page = 1;// 默认第1页
		start = (page - 1) * size;
		params.put("start", start);
		params.put("size", size);
		params.put("placeId", requestForm.getPlaceId());
		params.put("targetUser", requestForm.getUserId());
		int total = taskMapper.getTaskInfoCount(params);
		List<Task> taskList = taskMapper.getTaskInfoById(params);
		return ResponseJson.getInstance().setPage(page).setSize(size).setTotal(total).setData(taskList).toString();
	}

}
